<template>
  <div class="app-container">
    <div class="mb-22">
      <el-button v-hasPermi="['config:classify:add']" type="success" @click="handleAdd">添加分类</el-button>
    </div>

    <YxhyTableData :columns="columns" :data="tableList" :loading="loading">
      <!-- 插槽内容 -->
      <template #action>
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <div class="table-action-panel">
              <el-button v-hasPermi="['config:classify:edit']" type="success" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
              <!--              <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>-->
            </div>
          </template>
        </el-table-column>
      </template>
    </YxhyTableData>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryPage.pageNum"
      :limit.sync="queryPage.limit"
      @pagination="queryPageClassifyFn"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态" prop="status">
          <div class="flex ai-center">
            <el-switch v-model="form.status" />
            <p class="ml-10">{{ form.status ? '启用' : '禁用' }}</p>
          </div>
        </el-form-item>
        <el-form-item label="分类名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="备注" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { queryPageClassify, addClassify, editClassify, CLASSIFY_STATUS } from '@/api/config/classify.js'
import { columnsStatic } from './Ds.js'

export default {
  name: 'ClassifyList',
  data() {
    return {
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 分页参数
      queryPage: {
        pageNum: 1,
        limit: 10
      },
      loading: false,
      // 表格列
      columns: columnsStatic,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        status: true,
        title: undefined,
        intro: undefined
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: '请输入分类名称', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    isEdit() {
      return ![undefined, ''].includes(this.form.id)
    }
  },
  created() {
    this.queryPageClassifyFn()
  },
  methods: {
    async queryPageClassifyFn() {
      this.loading = true
      const res = await queryPageClassify({
        ...this.queryPage,
        ...this.queryParams
      })
      res.data.map(e => {
        const { status, intro } = e
        e.status_text = CLASSIFY_STATUS[status]
        e.intro = intro || '-'
      })
      this.tableList = res.data
      this.total = res.total
      this.loading = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        status: true,
        title: undefined,
        intro: undefined
      }
      this.resetForm('formRef')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryPage.pageNum = 1
      this.queryPageClassifyFn()
    },
    /** 清空搜索按钮操作 */
    handleClear() {
      this.queryParams = {
        library_id_search: undefined,
        title_search: undefined,
        classify_search: undefined,
        is_publish_search: undefined
      }
      this.queryPageClassifyFn()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加短剧分类'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const { id, status } = row

      this.reset()
      this.form = {
        ...row,
        classify_id: id,
        status: status == 1
      }
      this.open = true
      this.title = '修改短剧分类'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.form.status = this.form.status ? 1 : 2
          if (this.form.id != undefined) {
            editClassify(this.form).then(res => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.queryPageClassifyFn()
            })
          } else {
            addClassify(this.form).then(res => {
              this.$modal.msgSuccess('添加成功')
              this.open = false
              this.queryPageClassifyFn()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const {id} = row
      // const dictIds = row.dictId || this.ids
      // this.$modal
      //   .confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？')
      //   .then(function() {
      //     return delType(dictIds)
      //   })
      //   .then(() => {
      //     this.getList()
      //     this.$modal.msgSuccess('删除成功')
      //   })
      //   .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.item-box {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-top: 20px;
  .item-panel {
    width: 32%;
    background-color: #f8f8f8;
    padding: 10px;
    margin-bottom: 20px;
  }
}
.item-box:after{
  content: '';
  width: 30%;
}
</style>
